/*
 * Copyright (c) 2012 Johan Fylling
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights 
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
 * THE SOFTWARE.
 */

part of engine;


class CollidableRay extends Collidable {
  Vector2 target = new Vector2();
  
  CollidableRay ([Vector2 target]) {
    this.target = (?target ? target : new Vector2());
  }
  
  /** See [Collidable.boundingSphereRadius]. */
  num get boundingSphereRadius => this.target.length / 2;
  
  /** See [Collidable.getFoci]. */
  void getFoci (List<Vector2> foci, DynamicBody body) {
    // Rays don't have foci.
  }
  
  /** See [Collidable.getAxes]. */
  void getAxes (List<Vector2> axes, List<Vector2> foci, DynamicBody body) {
    axes.addLast(this.target.normalized());
    axes.addLast(new Vector2(-this.target.y, this.target.x).normalized());
    
    // Generate axes from foci.
    for (Vector2 focus in foci) {
      Vector2 axisA = focus - body.position;
      Vector2 axisB = focus - body.position + target;
      if (axisA.lengthSquared < axisB.lengthSquared) {
        axes.addLast(axisA.normalized());
      } else {
        axes.addLast(axisB.normalized());
      }
    }
  }
  
  /** See [Collidable.project]. */
  Interval project (Vector2 axis, DynamicBody body) {
    num dotProduct = axis.dot(body.position);
    num min = 0.0;
    num max = 0.0;
    
    dotProduct = axis.dot(body.position + this.target);
    if (dotProduct < min) {
      min = dotProduct;
    } else {
      max = dotProduct;
    }
    
    return new Interval(min, max);
  }
  
  /** See [Collidable.getFarthestFeature]. */
  ContactFeature getFarthestFeature (DynamicBody body, Vector2 direction) {
    Vector2 worldTarget = body.position + this.target;
    if (direction.dot(body.position) > direction.dot(worldTarget)) {
      return new ContactEdge(body.position, worldTarget, body.position);
    } else {
      return new ContactEdge(worldTarget, body.position, worldTarget);
    }
  }
  
  /** See [Collidable.raycast]. */
  bool raycast (Vector2 from, Vector2 to, DynamicBody body) {
    false;
  }
  
  /** See [Collidable.getRandomPoint]. */
  Vector2 getRandomPoint (DynamicBody body, Math.Random random) {
    // TODO: Calculate random point inside polygon.
    return body.position;
  }
  
  toJson () {
    var data = super.toJson();
    assert(data is Map);
    data["type"] = "collidable_ray";
    data["target"] = target;
    return data;
  }
}
